using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowDataTypes = ScenarioFramework.WorkflowEngine.DataModel.WorkflowDataTypes;
using WAPI = ScenarioFramework.WAPI;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per AssignParameter.
	/// </summary>
	public class AssignParameter : System.Windows.Forms.Form
	{
		public System.Windows.Forms.Label lblFunction;
		private System.Windows.Forms.RadioButton radioLiteral;
		public System.Windows.Forms.RadioButton radioBind;
		private System.Windows.Forms.TextBox txtLiteral;
		private System.Windows.Forms.ListView listAttributes;
		public System.Windows.Forms.Label lblParameter;
		public System.Windows.Forms.Label lblType;
		public System.Windows.Forms.Label lblMandatory;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;


		public bool isCancel = false;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox cmbValues;
		private System.Windows.Forms.RadioButton radioDataType;
		public string sAttribute = "";

		public AssignParameter()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.lblFunction = new System.Windows.Forms.Label();
            this.radioLiteral = new System.Windows.Forms.RadioButton();
            this.radioBind = new System.Windows.Forms.RadioButton();
            this.txtLiteral = new System.Windows.Forms.TextBox();
            this.listAttributes = new System.Windows.Forms.ListView();
            this.lblParameter = new System.Windows.Forms.Label();
            this.lblType = new System.Windows.Forms.Label();
            this.lblMandatory = new System.Windows.Forms.Label();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.cmbValues = new System.Windows.Forms.ComboBox();
            this.radioDataType = new System.Windows.Forms.RadioButton();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // lblFunction
            // 
            this.lblFunction.BackColor = System.Drawing.Color.White;
            this.lblFunction.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblFunction.Location = new System.Drawing.Point(7, 28);
            this.lblFunction.Name = "lblFunction";
            this.lblFunction.Size = new System.Drawing.Size(360, 24);
            this.lblFunction.TabIndex = 0;
            this.lblFunction.Text = "lblFunction";
            // 
            // radioLiteral
            // 
            this.radioLiteral.Checked = true;
            this.radioLiteral.Location = new System.Drawing.Point(7, 180);
            this.radioLiteral.Name = "radioLiteral";
            this.radioLiteral.Size = new System.Drawing.Size(153, 16);
            this.radioLiteral.TabIndex = 1;
            this.radioLiteral.TabStop = true;
            this.radioLiteral.Text = "Valore Letterale";
            // 
            // radioBind
            // 
            this.radioBind.Location = new System.Drawing.Point(7, 340);
            this.radioBind.Name = "radioBind";
            this.radioBind.Size = new System.Drawing.Size(391, 15);
            this.radioBind.TabIndex = 2;
            this.radioBind.Text = "Attributo ";
            // 
            // txtLiteral
            // 
            this.txtLiteral.Location = new System.Drawing.Point(7, 201);
            this.txtLiteral.Multiline = true;
            this.txtLiteral.Name = "txtLiteral";
            this.txtLiteral.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.txtLiteral.Size = new System.Drawing.Size(360, 132);
            this.txtLiteral.TabIndex = 3;
            // 
            // listAttributes
            // 
            this.listAttributes.Location = new System.Drawing.Point(7, 361);
            this.listAttributes.Name = "listAttributes";
            this.listAttributes.Size = new System.Drawing.Size(360, 224);
            this.listAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.listAttributes.TabIndex = 4;
            this.listAttributes.UseCompatibleStateImageBehavior = false;
            this.listAttributes.View = System.Windows.Forms.View.Details;
            this.listAttributes.SelectedIndexChanged += new System.EventHandler(this.listAttributes_SelectedIndexChanged);
            this.listAttributes.DoubleClick += new System.EventHandler(this.listAttributes_DoubleClick);
            // 
            // lblParameter
            // 
            this.lblParameter.BackColor = System.Drawing.Color.White;
            this.lblParameter.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblParameter.Location = new System.Drawing.Point(7, 76);
            this.lblParameter.Name = "lblParameter";
            this.lblParameter.Size = new System.Drawing.Size(360, 24);
            this.lblParameter.TabIndex = 5;
            this.lblParameter.Text = "lblParameter";
            // 
            // lblType
            // 
            this.lblType.BackColor = System.Drawing.Color.White;
            this.lblType.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblType.Location = new System.Drawing.Point(7, 125);
            this.lblType.Name = "lblType";
            this.lblType.Size = new System.Drawing.Size(266, 23);
            this.lblType.TabIndex = 6;
            this.lblType.Text = "lblType";
            this.lblType.Click += new System.EventHandler(this.lblType_Click);
            // 
            // lblMandatory
            // 
            this.lblMandatory.BackColor = System.Drawing.Color.White;
            this.lblMandatory.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.lblMandatory.Location = new System.Drawing.Point(287, 125);
            this.lblMandatory.Name = "lblMandatory";
            this.lblMandatory.Size = new System.Drawing.Size(80, 23);
            this.lblMandatory.TabIndex = 7;
            this.lblMandatory.Text = "lblMandatory";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 694);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(688, 42);
            this.panel2.TabIndex = 17;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(153, 7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(60, 35);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(67, 7);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(66, 35);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 14);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(126, 14);
            this.label1.TabIndex = 18;
            this.label1.Text = "Application";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(7, 62);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(140, 14);
            this.label2.TabIndex = 19;
            this.label2.Text = "Parameter";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(7, 111);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(83, 14);
            this.label3.TabIndex = 20;
            this.label3.Text = "Type";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(287, 104);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(83, 20);
            this.label4.TabIndex = 21;
            this.label4.Text = "Mandatory";
            // 
            // cmbValues
            // 
            this.cmbValues.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbValues.Enabled = false;
            this.cmbValues.Location = new System.Drawing.Point(113, 159);
            this.cmbValues.Name = "cmbValues";
            this.cmbValues.Size = new System.Drawing.Size(254, 21);
            this.cmbValues.TabIndex = 26;
            this.cmbValues.SelectedIndexChanged += new System.EventHandler(this.cmbValues_SelectedIndexChanged);
            // 
            // radioDataType
            // 
            this.radioDataType.Location = new System.Drawing.Point(7, 159);
            this.radioDataType.Name = "radioDataType";
            this.radioDataType.Size = new System.Drawing.Size(100, 16);
            this.radioDataType.TabIndex = 27;
            this.radioDataType.Text = "DataType";
            // 
            // AssignParameter
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(688, 736);
            this.Controls.Add(this.radioDataType);
            this.Controls.Add(this.cmbValues);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.lblMandatory);
            this.Controls.Add(this.lblType);
            this.Controls.Add(this.lblParameter);
            this.Controls.Add(this.listAttributes);
            this.Controls.Add(this.txtLiteral);
            this.Controls.Add(this.radioBind);
            this.Controls.Add(this.radioLiteral);
            this.Controls.Add(this.lblFunction);
            this.Name = "AssignParameter";
            this.Text = "AssignParameter";
            this.Load += new System.EventHandler(this.AssignParameter_Load);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		public void ShowAttributes(XMLAttributes vAttributes2Show)
		{
			listAttributes.Clear();

			listAttributes.Columns.Add("Name",listAttributes.Width - 200, HorizontalAlignment.Left);
			listAttributes.Columns.Add("Type",100, HorizontalAlignment.Left);
			listAttributes.Columns.Add("Direction",100, HorizontalAlignment.Left);

			for ( int i=0; i<vAttributes2Show.Count; i++)
			{
				XMLAttribute attribute = (XMLAttribute)vAttributes2Show[i];

				ListViewItem item = new ListViewItem( new string[] { attribute.Name, attribute.Type, attribute.Direction});
				listAttributes.Items.Add (item);
			}

			this.cmbValues.Items.Clear();

			#region DataTypes
			WorkflowDataTypes.XMLEnumerationValues vValues = null;
			
			try
			{
				vValues = WAPI.DataTypes.GetValues(this.lblType.Text);
			}
			catch ( WAPI.DataTypeException )
			{
			}

			if ( vValues != null )
			{
				WorkflowDataTypes.XMLDataTypeValue xmlValue = null;

				for ( int i=0; i<vValues.Count; i++)
				{
					xmlValue = (WorkflowDataTypes.XMLDataTypeValue)vValues[i];

					this.cmbValues.Items.Add ( xmlValue.Key);
				}

				this.cmbValues.Enabled = true;
			}
			else
			{
				this.cmbValues.Enabled = false;
			}

			#endregion
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.isCancel=true;
			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			if ( this.radioLiteral.Checked )
			{
				sAttribute = "<![CDATA[" + this.txtLiteral.Text + "]]>";
			}
			else if ( this.radioDataType.Checked )
			{
				sAttribute = "[" + cmbValues.Text + "]"; 
			}
			else
			{
				sAttribute = "[" + listAttributes.SelectedItems[0].SubItems[0].Text + "]"; 
			}
			this.Close();
		}

		private void listAttributes_DoubleClick(object sender, System.EventArgs e)
		{
			this.radioBind.Checked = true;
			this.btnSave_Click(btnSave, null);
		}

		private void lblType_Click(object sender, System.EventArgs e)
		{
		
		}

		private void cmbValues_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			#region DataTypes
			WorkflowDataTypes.XMLEnumerationValues vValues = null;
			
			try
			{
				vValues = WAPI.DataTypes.GetValues(this.lblType.Text);
			}
			catch ( WAPI.DataTypeException )
			{
			}

			if ( vValues != null )
			{
				WorkflowDataTypes.XMLDataTypeValue xmlValue = null;

				for ( int i=0; i<vValues.Count; i++)
				{
					xmlValue = (WorkflowDataTypes.XMLDataTypeValue)vValues[i];

					if ( xmlValue.Key == cmbValues.Text ) 
					{
						this.txtLiteral.Text = xmlValue.Value;
					}
				}
			}
			#endregion

		}

		private void AssignParameter_Load(object sender, System.EventArgs e)
		{
			if ( sAttribute.CompareTo ( string.Empty ) != 0 )
			{
				{
					this.radioLiteral.Checked = true;
					this.txtLiteral.Text = sAttribute;
				}
			}
		}

		private void listAttributes_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
